package cn.xabad.commons.tools;

import cn.xabad.commons.io.FileUtils;
import cn.xabad.commons.io.IOUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.crypto.CipherInputStream;


// +----------------------------------------------------------------------
// | CreateTime: 15/6/18 
// +----------------------------------------------------------------------
// | Author:     xab(http://www.xueyong.net.cn)
// +----------------------------------------------------------------------
// | CopyRight:  http://www.boxfish.cn
// +----------------------------------------------------------------------
public class FileU extends FileUtils {

    public static void deleteFile(File file) {
        if (!file.exists()) return;
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                File source = files[i];
                File to = new File(files[i].getAbsolutePath() + System.nanoTime());
                source.renameTo(to);
                deleteFile(to);
                to.delete();
            }
        } else {
            File to = new File(file.getAbsolutePath() + System.nanoTime());
            file.renameTo(to);
            deleteFile(to);
            to.delete();
        }
    }

    public static String getSavePath(String filename) {
        String one = filename.substring(0, 2);
        String two = filename.substring(2, 3);
        String filePath = File.separator + one + File.separator + two + File.separator;
        return filePath + filename;
    }


    public static String convertFileSize(long size) {
        long kb = 1024;
        long mb = kb * 1024;
        long gb = mb * 1024;

        if (size >= gb) {
            return String.format("%.1f GB", (float) size / gb);
        } else if (size >= mb) {
            float f = (float) size / mb;
            return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
        } else if (size >= kb) {
            float f = (float) size / kb;
            return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
        } else
            return String.format("%d B", size);
    }

    public static void createFile(File file) {
        if (!file.exists()) {
            try {
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                file.createNewFile();
            } catch (IOException e) {
            }
        }
    }

    public static boolean isExist(String filePath) {
        File file = new File(filePath);
        return file.exists();
    }

    public static boolean isExist(String fileDir, String fileName) {
        File file = new File(fileDir, fileName);
        return file.exists();
    }

    public static boolean ifNotExistCreateFile(String fileDir, String fileName) {
        File file = new File(fileDir, fileName);
        if (!file.exists()) {
            try {
                return file.createNewFile();
            } catch (IOException e) {
                return false;
            }
        }
        return true;
    }

    public static boolean ifNotExistCreateFile(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            try {
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                return file.createNewFile();
            } catch (IOException e) {
                return false;
            }
        }
        return true;
    }

    public static boolean ifNotExistCreateFile(File file) {
        if (!file.exists()) {
            try {
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                return file.createNewFile();
            } catch (IOException e) {
                return false;
            }
        }
        return true;
    }

    public static boolean ifNotExistCreateDir(String fileDir) {
        File file = new File(fileDir);
        if (!file.exists()) {
            return file.mkdirs();
        }
        return true;
    }

    public static void writeStreamToFile(String filePath, InputStream in) throws IOException {
        File file = null;
        FileOutputStream out = null;
        try {
            if (!ifNotExistCreateFile(filePath)) {
                return;
            }
            file = new File(filePath);
            out = new FileOutputStream(file);
            IOUtils.copyLarge(in, out);
        } finally {
            IOUtils.closeQuietly(out);
            IOUtils.closeQuietly(in);
        }
    }

    public static void writeDataToFile(String fileName, String data) throws Exception {
        OutputStream out = null;
        File file = null;
        InputStream inputStream = null;
        CipherInputStream cis = null;
        try {
            file = new File(fileName);
            out = openOutputStream(file);
            inputStream = IOUtils.toInputStream(data);
            cis = new CipherInputStream(inputStream, CipherU.getEncrypt());
            IOUtils.copyLarge(cis, out);
        } finally {
            IOUtils.closeQuietly(inputStream);
            IOUtils.closeQuietly(cis);
            IOUtils.closeQuietly(out);
        }
    }

    public static void writeStringToFile(String filePath, String data) {
        InputStream inputStream = null;
        File file = null;
        FileOutputStream out = null;
        try {
            if (!ifNotExistCreateFile(filePath)) {
                return;
            }
            file = new File(filePath);
            out = openOutputStream(file);
            inputStream = IOUtils.toInputStream(data);
            out = new FileOutputStream(file);
            IOUtils.copyLarge(inputStream, out);
        } catch (Exception e) {
        } finally {
            IOUtils.closeQuietly(out);
            IOUtils.closeQuietly(inputStream);
        }
    }

    public static void writeDataToFile(String filePath, InputStream in) throws Exception {
        CipherInputStream cis = null;
        try {
            cis = new CipherInputStream(in, CipherU.getEncrypt());
            writeStreamToFile(filePath, cis);
        } finally {
            IOUtils.closeQuietly(cis);
            IOUtils.closeQuietly(in);
        }
    }

    public static String readDataFromFile(String fileName) throws Exception {
        InputStream in = null;
        File file = null;
        CipherInputStream cis = null;
        try {
            file = new File(fileName);
            in = openInputStream(file);
            cis = new CipherInputStream(in, CipherU.getDecrypt());
            return IOUtils.toString(cis);
        } finally {
            IOUtils.closeQuietly(cis);
            IOUtils.closeQuietly(in);
        }
    }
}
